﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Data.Storage;
using Joel.Net;

namespace ContourStrikesAgain.RecordSetActionTypes
{
    public class MarkRecordSetAsHam : RecordsetActionType
    {
        public MarkRecordSetAsHam()
        {
            this.Description = "Mark records as ham";
            this.Id = new Guid("95D5B181-5659-4A6F-A716-A1B1379B7139");
            this.Name = "Mark as ham (not spam)";
        }

        public override List<Exception> ValidateSettings()
        {
            List<Exception> exceptions = new List<Exception>();

            return exceptions;
        }

        public override Umbraco.Forms.Core.Enums.RecordActionStatus Execute(List<Record> records, Form form)
        {
            WorkflowStorage wfs = new WorkflowStorage();
            var spamWF = wfs.GetAllWorkFlows(form).Where(w => w.Type.Id == ProviderIDCollection.akismetCommentSpamProtectionProviderGuid).FirstOrDefault();

            if (spamWF != null)
            {
                SettingsStorage ss = new SettingsStorage();
                var settings = ss.GetSettings(spamWF.Id);

                if (settings != null && settings.Count > 0)
                {
                    string akismetAPIKey = settings.Where(s => s.Key == "AkismetAPIKey").FirstOrDefault().Value;
                    string contentField = settings.Where(s => s.Key == "ContentField").FirstOrDefault().Value;
                    string authorField = settings.Where(s => s.Key == "AuthorField").FirstOrDefault().Value;
                    string authorEmailField = settings.Where(s => s.Key == "AuthorEmailField").FirstOrDefault().Value;
                    string authorUrlField = settings.Where(s => s.Key == "AuthorUrlField").FirstOrDefault().Value;
                    string spamField = settings.Where(s => s.Key == "SpamField").FirstOrDefault().Value;

                    foreach (Record record in records)
                    {
                        //update spam field
                        var spamFld = record.RecordFields.Values.Where(x => x.Field.Id == new Guid(spamField)).FirstOrDefault();

                        if (spamFld != null)
                        {
                            spamFld.Values.Clear();
                            spamFld.Values.Add(false);

                            RecordStorage rs = new RecordStorage();
                            rs.UpdateRecord(record, form);
                            rs.UpdateRecordXml(record, form);
                            rs.Dispose();
                        }

                        //Send status back to akismet
                        var author = record.RecordFields.Values.Where(x => x.Field.Id == new Guid(authorField)).FirstOrDefault().ValuesAsString();
                        var authorEmail = record.RecordFields.Values.Where(x => x.Field.Id == new Guid(authorEmailField)).FirstOrDefault().ValuesAsString();
                        var authorUrl = record.RecordFields.Values.Where(x => x.Field.Id == new Guid(authorUrlField)).FirstOrDefault().ValuesAsString();
                        var content = record.RecordFields.Values.Where(x => x.Field.Id == new Guid(contentField)).FirstOrDefault().ValuesAsString();

                        var api = new Akismet(
                           akismetAPIKey,
                           "http://" + HttpContext.Current.Request.ServerVariables["HTTP_HOST"],
                           "Umbraco Contour");
                        if (api != null)
                        {
                            var comment = new AkismetComment
                            {
                                CommentType = "comment",
                                CommentAuthor = author,
                                CommentAuthorEmail = authorEmail,
                                CommentAuthorUrl = authorUrl,
                                CommentContent = content,
                                UserIp = record.IP
                            };
                            api.SubmitHam(comment);
                        }
                    }
                    return Umbraco.Forms.Core.Enums.RecordActionStatus.Completed;
                }
            }

            return Umbraco.Forms.Core.Enums.RecordActionStatus.Failed;
        }
    }
}